package actor
{
	import actor.attribute.TowerAttribute;
	import actor.controller.TowerAttackController;
	import actor.controller.TowerImageController;
	import actor.controller.TowerInteractiveController;
	import actor.controller.TowerUpgradeController;
	import actor.event.TowerEvent;
	
	import capricornus.actor.Actor;
	import capricornus.actor.controller.ActorImageController;
	import capricornus.actor.utils.ActionType;
	import capricornus.components.utils.MovieClipUtils;
	import capricornus.components.utils.tipmotion.TipMotionController;
	import capricornus.utils.MathUtils;
	
	import config.DemonConfig;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.text.TextField;
	
	import res.DefaultResManager;
	
	import scene.Scene;
	
	public class Tower extends Actor
	{
		private var _upgradeController:TowerUpgradeController;
		private var _attackController:TowerAttackController;
		
		private var _gridI:int;
		private var _gridJ:int;
		
		private var _scene2:Scene;
		
		public function Tower()
		{
			super();
			init();
		}
		
		public static function createTowerByID( id:int ):Tower
		{
			var xml:XML = DemonConfig.instance.getTowerDataByID( id );
			var t:Tower = new Tower();
			
			var att:TowerAttribute = t.attribute as TowerAttribute;
			att.setID( MathUtils.getUniqueID() );
			att.setLevel( int(xml.@level) );
			att.setActionImagePath( xml.@image );
			att.setBulletID( int(xml.@bullet) );
			att.setHeight( int(xml.@height) );
			att.setUseGrid( int(xml.@useGrid) );
			
			var uc:TowerUpgradeController = t.upgradeController;
			uc.setAttackSpeedArr( String(xml.@speed).split( "|" ) );
			uc.setDamageArr( String(xml.@damage).split( "|" ) );
			uc.setRangeArr( String(xml.@range).split( "|" ) );
			uc.setUpgradeTimeArr( String(xml.@upgradeTime).split( "|" ) );
			uc.setDelayTimeArr( String(xml.@delayTime).split( "|" ) );
			uc.setBulletCountArr( String(xml.@bulletCount).split( "|" ) );
			
			uc.setCriticalStrikeRate( String(xml.@criticalStrikeRate).split( "|" ) );
			uc.setCriticalStrikeMulti( String(xml.@criticalStrikeMulti).split( "|" ) );
			uc.setStunRate( String(xml.@stunRate).split( "|" ) );
			uc.setStunDamage( String(xml.@stunDamage).split( "|" ) );
			uc.setStunDuration( String(xml.@stunDuration).split( "|" ) );
			
			uc.upgradeComplete();
			
			t.scaleX = 0.4;
			t.scaleY = 0.4;
			t.activate();
			t.stand();
			
			return t;
		}
		
		private function init():void
		{
			_attackController = new TowerAttackController( this );
			_upgradeController = new TowerUpgradeController( this );
		}
		
		override public function destroy():void
		{
			super.destroy();
			_scene2 = null;
		}
		
		override public function refresh(time:uint):void
		{
			super.refresh( time );
			
			_attackController.refresh( time );
		}
		
		public function get gridI():int
		{
			return _gridI;
		}
		
		public function get gridJ():int
		{
			return _gridJ;
		}
		
		public function attack():void
		{
			_imageController.playAction( ActionType.FIGHT, 1, ActionType.STAND );
		}
		
		public function setGridInfo( i:int, j:int ):void
		{
			_gridI = i;
			_gridJ = j;
		}
		
		public function stopFire():void
		{
			_attackController.stop();
		}
		
		public function setScene( scene:Scene ):void
		{
			_scene2 = scene;
		}
		
		public function showCriticalStrikeTips( damage:int ):void
		{
			var s:Sprite = DefaultResManager.getNumberDisplay( damage );
			s.y = this.y -_attribute.height + 10;
			s.x = this.x + 20;
			var tc:TipMotionController = new TipMotionController( s );
			tc.onComplete = onTipComplete;
			tc.removeOnComplete = true;
			
			_scene2.tipLayer.addChild( s );
		}
		
		private function onTipComplete( s:Sprite ):void
		{
			MovieClipUtils.removeMC( s );
		}
		
		public function get upgradeController():TowerUpgradeController
		{
			return _upgradeController;
		}
		
		override protected function createAttribute():void
		{
			_attribute = new TowerAttribute();
		}
		
		override protected function createImageController():void
		{
			_imageController = new TowerImageController( this );
		}
		
		override protected function createInteractiveController():void
		{
			_interactiveController = new TowerInteractiveController( this );
		}
		
		override protected function sort( container:DisplayObjectContainer, a:Actor ):void
		{
			if( container==null ) return;
			var len:int = container.numChildren;
			var i:int;
			var dis:DisplayObject;
			var y:int = a.y;
			var x:int = a.x;
			var index:int = container.getChildIndex( a );
			var b:Boolean = true;
			var toIndex:int = index;
			
			for( i=index+1; i < len; i++ )
			{
				dis = container.getChildAt(i);
				if( dis.y < y )
				{
					b = false;
					toIndex = i;
				}
				else if( dis.y==y )
				{
					if( dis.x < x )
					{
						b = false;
						toIndex = i;
					}
				}
				else break;
			}
			if( b )
			{
				for( i = index - 1; i > -1; i-- )
				{
					dis = container.getChildAt(i);
					if( dis.y > y )
					{
						toIndex = i;
					}
					else if( dis.y==y )
					{
						if( dis.x > x )
						{
							toIndex = i;
						}
					}
					else break;
				}
			}
			
			if( index != toIndex )
			{
				container.setChildIndex( a, toIndex );
			}
		}
	}
}